package com.health.ts.utils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import freemarker.cache.MruCacheStorage;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

/**
 * Created on 2017年9月19日
 * <p>
 * Description {模板工具类}
 * <p>
 *
 */
public class TempleteUtil {
	/**
	 * freemarker配置类
	 */
	private static final Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
	static {
		configuration.setDefaultEncoding("UTF-8");
		configuration.setClassForTemplateLoading(TempleteUtil.class, "/");
		configuration.setCacheStorage(new MruCacheStorage(10, 50));
	}

	private TempleteUtil() {
	}

	/**
	 * Created on 2017年9月20日 .
	 * 
	 * @author Administrator
	 * @param templatePath
	 * @param dataMap
	 *            {描述}
	 * @throws Exception
	 */
	public static String process(String templatePath, Map<String, Object> dataMap) throws Exception {
		Writer writer = new StringWriter();
		process(templatePath, writer, dataMap);
		return writer.toString();
	}
	
	/**
	 * Created on 2017年9月20日 .
	 * 
	 * @author Administrator
	 * @param templatePath
	 * @param os
	 * @param dataMap
	 *            {描述}
	 * @throws Exception
	 */
	public static void process(String templatePath, OutputStream os, Map<String, Object> dataMap) throws Exception {
		process(templatePath, new BufferedWriter(new OutputStreamWriter(os, "UTF-8")), dataMap);
	}
	
	/**
	 * Created on 2017年9月20日 .
	 * 执行模板
	 * @author Administrator
	 * @param templatePath
	 * @param os
	 * @param dataMap
	 *            {描述}
	 * @throws Exception
	 */
	public static void process(String templatePath, Writer writer, Map<String, Object> dataMap) throws Exception {
		Template template = getTemplate(templatePath);
		porcess(template, writer, dataMap);
	}

	/**
	 * Created on 2017年9月20日 .
	 * 执行模板
	 * @author Administrator
	 * @param template
	 * @param writer
	 * @param dataMap
	 * @throws TemplateException
	 * @throws IOException {描述}
	 */
	private static void porcess(Template template, Writer writer, Map<String, Object> dataMap) throws TemplateException, IOException {
		try {
			template.process(dataMap, writer);
			writer.flush();
		} finally {
			if (writer != null) {
				writer.close();
				writer = null;
			}
		}
	}

	/**
	 * Created on 2017年9月20日 . 获取模板
	 * 
	 * @author Administrator
	 * @param templatePath
	 * @return
	 * @throws TemplateNotFoundException
	 * @throws MalformedTemplateNameException
	 * @throws ParseException
	 * @throws IOException
	 *             {描述}
	 */
	private static Template getTemplate(String templatePath) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException {
		return configuration.getTemplate(templatePath);
	}
}
